package fr.umlv.m2.jee.spij.service.attribution.impl;

import fr.umlv.m2.jee.spij.persistence.actor.TeacherUnit;
import fr.umlv.m2.jee.spij.persistence.actor.dao.HibernateTeacherUnitDAO;
import fr.umlv.m2.jee.spij.persistence.education.dao.HibernateUnitDAO;
import fr.umlv.m2.jee.spij.service.attribution.DisplayAttribution;
import fr.umlv.m2.jee.spij.service.attribution.IDisplayAttributionService;
import fr.umlv.m2.jee.spij.service.ldap.authentification.DisplayActor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("defaultDisplayAttributionService")

public class DefaultDisplayAttributionService implements
        IDisplayAttributionService {
    @Autowired
    private HibernateTeacherUnitDAO teacherUnitDAO;

    @Autowired
    private HibernateUnitDAO unitDAO;


    /**
     * @see fr.umlv.m2.jee.spij.service.attribution.IDisplayAttributionService#createAttribution(DisplayActor, DisplayAttribution, MessageContext)
     */
    @Transactional(readOnly = false)
    @Override
    public boolean createAttribution(DisplayActor teacher, DisplayAttribution attribution,
                                     MessageContext context) {
        TeacherUnit teacherAttribution = new TeacherUnit();
        teacherAttribution.setTeacherLogin(teacher.getLogin());
        teacherAttribution.setUnit(unitDAO
                .findByName(attribution.getUnitName()).getId());
        teacherUnitDAO.persist(teacherAttribution);
        return true;
    }

    /**
     * @param teacher
     * @param context
     *
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<DisplayAttribution> getAllDisplayAttributionForTeacher(
            DisplayActor teacher, MessageContext context) {
        List<TeacherUnit> teacherUnits = teacherUnitDAO.findByTeacherLogin(teacher.getLogin());
        List<DisplayAttribution> listAttributions = new ArrayList<DisplayAttribution>();

        if (teacherUnits == null) {
            context.addMessage(new MessageBuilder().info().code("units.empty").build());
        } else {
            for (TeacherUnit unit : teacherUnits) {
                listAttributions.add(new DisplayAttribution(unit, unitDAO
                        .findById(unit.getUnit()).getName()));
            }
        }
        return listAttributions;
    }

    /**
     * @see fr.umlv.m2.jee.spij.service.attribution.IDisplayAttributionService#removeAttribution(DisplayAttribution, MessageContext)
     */
    @Transactional(readOnly = false)
    @Override
    public boolean removeAttribution(DisplayAttribution attribution,
                                     MessageContext context) {
        TeacherUnit teacherUnit = teacherUnitDAO.findById(attribution.getId());
        teacherUnitDAO.remove(teacherUnit);
        return true;
    }

}
